﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.Common;
using System.Web.Profile;
using System.Web.Security;

namespace smartphone.App_Code
{
    public class ShoppingCart
    {
        private string shoppingcartID
        {
            get
            {
                //lay httpcontext hien tai
                HttpContext context = HttpContext.Current;
                
                    string cartID;
                    //Lay doi cookie qua clas Request Neu cartId ton tai trong cookie 
                    if (context.Request.Cookies["Esmart_cartID"] != null)
                    {
                        cartID = context.Request.Cookies["Esmart_cartID"].Value;
                        return cartID;
                    }
                    //neu cartId khong co trong cookie, tao mot cartId moi
                    else
                    {
                        //tao mot GUID MOI
                        cartID = Guid.NewGuid().ToString();
                        //khoi tao 1 cookiemoi
                        HttpCookie cookie = new HttpCookie("Esmart_cartID", cartID);
                        //so ngay cookie expire
                        int day = ESmartConfig.CartPersistDays;
                        DateTime currentDate = DateTime.Now;
                        TimeSpan timeSpan = new TimeSpan(day, 0, 0, 0);
                        //ngay cookie het hang la 10 ngay (duoc thiet lap trong webcofig)
                        DateTime expirationDate = currentDate.Add(timeSpan);
                        cookie.Expires = expirationDate;
                        //them cookie vao trinh duyet may khach qua class Response
                        context.Response.Cookies.Add(cookie);

                        //tra ve ma gio hang
                        return cartID.ToString();
                    }
            }
        }

        /// <summary>
        /// Them san pham vao gio hang
        /// </summary>
        /// <param name="maSanPham"></param>
        /// <param name="thuocTinh"></param>
        /// <returns></returns>
        public bool AddItem(string maSanPham, string thuocTinh)
        {
            DbCommand comm = GenericDataAccess.CreateCommand();
            comm.CommandText = "ShoppingCartAddItem";

            DbParameter param = comm.CreateParameter();
            param.ParameterName = "@maGioHang";
            param.Value = shoppingcartID;
            param.DbType = DbType.String;
            param.Size = 36;
            comm.Parameters.Add(param);

            param = comm.CreateParameter();
            param.ParameterName = "@maSanPham";
            param.Value = maSanPham;
            param.DbType = DbType.Int32;
            comm.Parameters.Add(param);

            param = comm.CreateParameter();
            param.ParameterName = "@thuocTinh";
            param.Value = thuocTinh;
            param.DbType = DbType.String;
            comm.Parameters.Add(param);

            try
            {
                return (GenericDataAccess.ExecuteNonQuery(comm) != -1);
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// Cap nhat gio hang
        /// </summary>
        /// <param name="maSanPham"></param>
        /// <param name="thuocTinh"></param>
        /// <returns></returns>
         public bool UpdateItem(string maSanPham, int soLuong)
        {
            DbCommand comm = GenericDataAccess.CreateCommand();
            comm.CommandText = "ShoppingCartUpdateItem";

            DbParameter param = comm.CreateParameter();
            param.ParameterName = "@maGioHang";
            param.Value = shoppingcartID;
            param.DbType = DbType.String;
            param.Size = 36;
            comm.Parameters.Add(param);

            param = comm.CreateParameter();
            param.ParameterName = "@maSanPham";
            param.Value = maSanPham;
            param.DbType = DbType.Int32;
            comm.Parameters.Add(param);

            param = comm.CreateParameter();
            param.ParameterName = "@soLuong";
            param.Value = soLuong;
            param.DbType = DbType.Int32;
            comm.Parameters.Add(param);

            try
            {
                return (GenericDataAccess.ExecuteNonQuery(comm) != -1);
            }
            catch
            {
                return false;
            }
        }


        /// <summary>
        /// Xoa san pham trong gio hang
        /// </summary>
        /// <param name="maSanPham"></param>
        /// <returns></returns>
         public bool RemoveItem(string maSanPham)
         {
             DbCommand comm = GenericDataAccess.CreateCommand();
             comm.CommandText = "ShoppingCartRemoveItem";

             DbParameter param = comm.CreateParameter();
             param.ParameterName = "@maGioHang";
             param.Value = shoppingcartID;
             param.DbType = DbType.String;
             param.Size = 36;
             comm.Parameters.Add(param);

             param = comm.CreateParameter();
             param.ParameterName = "@maSanPham";
             param.Value = maSanPham;
             param.DbType = DbType.Int32;
             comm.Parameters.Add(param);

             try
             {
                 return (GenericDataAccess.ExecuteNonQuery(comm) != -1);
             }
             catch
             {
                 return false;
             }
         }


        /// <summary>
        /// Lay cac san pham trong gio hang
        /// </summary>
        /// <returns></returns>
         public DataTable GetItems()
         {
             DbCommand comm = GenericDataAccess.CreateCommand();
             comm.CommandText = "ShoppingCartGetItems";

             DbParameter param = comm.CreateParameter();
             param.ParameterName = "@maGioHang";
             param.Value = shoppingcartID;
             param.DbType = DbType.String;
             param.Size = 36;
             comm.Parameters.Add(param);

             return GenericDataAccess.ExecuteSelectCommand(comm);
         }

        /// <summary>
        /// Lay tong so tien can pham chi tra cho gio hang
        /// </summary>
        /// <returns></returns>
         public decimal GetTotalAmount()
         {
             DbCommand comm = GenericDataAccess.CreateCommand();
             comm.CommandText = "ShoppingCartGetTotalAmount";

             DbParameter param = comm.CreateParameter();
             param.ParameterName = "@maGioHang";
             param.Value = shoppingcartID;
             param.DbType = DbType.String;
             param.Size = 36;
             comm.Parameters.Add(param);

             return Decimal.Parse(GenericDataAccess.ExecuteScalar(comm));
         }

        /// <summary>
        /// Dem so luong gio hang da het han
        /// </summary>
        /// <param name="days">So ngay dem</param>
        /// <returns></returns>
         public int CountOldCarts(byte days)
         {
             DbCommand comm = GenericDataAccess.CreateCommand();
             comm.CommandText = "ShoppingCartCountOldCarts";

             DbParameter param = comm.CreateParameter();
             param.ParameterName = "@soNgay";
             param.Value = days;
             param.DbType = DbType.Byte;
             comm.Parameters.Add(param);

             try
             {
                 return Int32.Parse(GenericDataAccess.ExecuteScalar(comm));
             }
             catch
             {
                 return -1;
             }
         }

        /// <summary>
        /// Xoa gio hang da het han
        /// </summary>
        /// <param name="days">So ngay dem</param>
        /// <returns></returns>
         public bool DeleteOldCarts(byte days)
         {

             DbCommand comm = GenericDataAccess.CreateCommand();
             comm.CommandText = "ShoppingCartDeleteOldCarts";

             DbParameter param = comm.CreateParameter();
             param.ParameterName = "@soNgay";
             param.Value = days;
             param.DbType = DbType.Byte;
             comm.Parameters.Add(param);

             try
             {
                 return GenericDataAccess.ExecuteNonQuery(comm) != -1;
             }
             catch
             {
                 return false;
             }
         }

        /// <summary>
        /// Tao don hang va tra ve ma don hang
        /// </summary>
        /// <returns>Ma don hang duoc tao</returns>
         public string CreateOrder()
         {
             DbCommand comm = GenericDataAccess.CreateCommand();
             comm.CommandText = "CreateOrder";

             DbParameter param = comm.CreateParameter();
             param.ParameterName = "@maGioHang";
             param.Value = shoppingcartID;
             param.DbType = DbType.String;
             param.Size = 36;

             comm.Parameters.Add(param);

             return GenericDataAccess.ExecuteScalar(comm);
         }

        /// <summary>
        /// Tao don hang bao gom ca ten khach hang va tra ve ma don hang
        /// </summary>
        /// <returns></returns>
         public string CreateCommerceLibOrder(int taxId)
         {
             DbCommand comm = GenericDataAccess.CreateCommand();
             comm.CommandText = "CreateCustomerOrder";

             DbParameter param = comm.CreateParameter();
             param.ParameterName = "@maGioHang";
             param.Value = shoppingcartID;
             param.DbType = DbType.String;
             param.Size = 36;
             comm.Parameters.Add(param);

             param = comm.CreateParameter();
             param.ParameterName = "@maKhachHang";
             param.Value = Membership.GetUser(HttpContext.Current.User.Identity.Name).ProviderUserKey;
             param.DbType = DbType.Guid;
             comm.Parameters.Add(param);

             param = comm.CreateParameter();
            param.ParameterName = "@maThue";
            param.Value = taxId;
            param.DbType = DbType.Int32;
            comm.Parameters.Add(param);

             return GenericDataAccess.ExecuteScalar(comm);
         }
    }
}